define(['zepto', 'backbone', 'vendor/hammer/js/hammer'], function($, Backbone, na) {

    var Gesture = Backbone.Model.extend({

    }, {
        options: {
            observers: [],
            maxScale: 1,
            minScale: 1,
            onZoom: null,
            onTransformStart: null,
            onTransformEnd: null
        },

        bindPinch: function(el, target, options) {
            var me = this;
            var key;
            for (key in options) {
                if (key in this) this[key] = options[key];
            }
            var obj = {};
            for (var configKey in this.options) {
                obj[configKey] = this.options[configKey];
            }
            for (var argkey in options) {
                obj[argkey] = options[argkey];
            }
            options = obj;

            //基础大小
            console.log(target)
            var baseWidth = target.width ? target.width : $(target).width();
            var baseHeight = target.height ? target.height : $(target).height();
            //最大值
            var maxWidth = target.width ? target.width * options.maxScale : $(target).width() * options.maxScale;
            var maxHeight = target.height ? target.height * options.maxScale : $(target).height() * options.maxScale;
            //最小值
            var minWidth = target.width ? target.width * options.minScale : $(target).width() * options.minScale;
            var minHeight = target.height ? target.height * options.minScale : $(target).height() * options.minScale;


            var preScale = 0;
            var isMoving = false;
            var scale;
            var tempScale = 0;

            var hammertime = Hammer(el).on("pinch", function(event) {
                if (!isMoving) {
                    tempScale = null;
                }
                scale = event.gesture.scale;
                isMoving = true;
                var originScale = options.originScale;
                var originSize = options.originSize;
                if (tempScale != scale && tempScale != null) {

                    // if(event.gesture.scale > 1){
                    //     scale = 1 + (event.gesture.scale - 1) * 0.3;
                    // }else{
                    //     scale = (event.gesture.scale - 1) * 0.3+ 1;
                    // }

                    if (target.tagName == "CANVAS") {
                        //canvas
                        target.width = target.width + 300 * (scale - tempScale);
                        target.height = target.height + 300 * (scale - tempScale);

                        if (target.height < minHeight) {
                            target.width = minWidth;
                            target.height = minHeight;
                        } else if (target.height > maxHeight) {
                            target.width = maxWidth;
                            target.height = maxHeight;
                        }
                    } else {
                        //普通元素
                        var finalScal = originScale*(1+ (scale-1)*0.3);//0.7 + 0.3 * scale;
                        // var newLeft = parseInt((finalScal-1)*originSize.width/2);
                        // var newTop = parseInt((finalScal-1)*originSize.height/2);
                        $(el).find("#tit").html("width = "+ newLeft +" --- " + "top = "+newTop)

                        if (finalScal > options.minScale && finalScal < options.maxScale) {
                            $(target).css({
                                '-webkit-transform': 'scale(' + finalScal + ',' + finalScal + ')'
                                // ,
                                // 'position': 'relative',
                                // 'left':newLeft,
                                // 'top':newTop
                            });
                        }

                    }

                    if (options.onZoom) { //回调缩放函数
                        options.onZoom(event, target.width ? target.width : $(target).width(), target.height ? target.height : $(target).height());
                    }

                }
                tempScale = scale;
            });

            var timerId = setInterval(function(ev) {
                if (scale != preScale) {
                    preScale = scale;
                } else {
                    if (isMoving) {
                        options.onTransformEnd();
                        // console.log("temp->"+tempScale+",now->"+scale);
                        isMoving = false;
                    }
                }
            }, 150);

            if (options.onTransformStart) {
                hammertime.on("transformstart", function() {
                    options.onTransformStart();
                })
            }

            return hammertime;
        },

        bindDrag: function(el, target, options) {
            var defaultOption = {
                orginalPosition: {
                    left: 0,
                    top: 0
                },
                maxRight: 0,
                maxTop: 0,
                maxBottom: 0
            };

            for (key in options) {
                defaultOption[key] = options[key];
            }

            var hammertime = Hammer(el).on("drag", function(event) {
                console.info('left');
                console.log(options.view.orginalPosition.left)
                $(target).css({
                    'left': options.view.orginalPosition.left + event.gesture.deltaX,
                    'top': options.view.orginalPosition.top + event.gesture.deltaY,
                    'position': 'relative'
                });


                if (options.onDrag) { //回调拖拉函数
                    options.onDrag(event, target.width ? target.width : $(target).width(), target.height ? target.height : $(target).height());
                }
            });



            if (options.onTransformStart) {
                hammertime.on("dragstart", function() {

                    options.onTransformStart(options.view);
                });
            };

            return hammertime;

        }
    });

    return Gesture;
});